package com.dicsystems.myrec;

import java.util.Calendar;
import java.util.List;

import com.dicsystems.myrec.dao.TodoDao;
import com.dicsystems.myrec.dto.Todo;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

public class DailyListActivity extends ListActivity {

	private Context me = this;
	private Common co = new Common(me);
	private TodoDao td = new TodoDao(me);
	private Calendar date;
	private TextView title;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.daily_list);
		date = (Calendar)getIntent().getSerializableExtra("date");
		title = (TextView)findViewById(R.id.title);
	}

	@Override
	protected void onResume() {
		super.onResume();
		td.open();
		final List<Todo> todos = td.select(date);
		getListView().setAdapter(new TodoAdapter(me, R.layout.daily_list_row, todos));
		getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent intent = new Intent(me, DetailsActivity.class);
				intent.putExtra("date", date);
				intent.putExtra("todo", todos.get(position));
				startActivity(intent);
			}
		});
		if(date.compareTo(co.today()) < 0) {
			title.setText(co.date(date));
		}
		else {
			title.setText(co.date(date) + getResources().getString((R.string.daily_list_title)));
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		td.close();
	}

	public void add(View view) {
		startActivity(new Intent(me, EditActivity.class).putExtra("date", date));
	}

	public void back(View view) {
		startActivity(new Intent(me, HomeActivity.class));
	}
}
